package org.jivesoftware.smack;

import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import org.jivesoftware.smack.debugger.SmackDebugger;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;

public abstract class Connection
{
  public static boolean DEBUG_ENABLED;
  private static final AtomicInteger connectionCounter = new AtomicInteger(0);
  private static final Set<ConnectionCreationListener> connectionEstablishedListeners = new CopyOnWriteArraySet();
  private AccountManager accountManager;
  private ChatManager chatManager;
  protected final Collection<PacketCollector> collectors;
  protected final ConnectionConfiguration config;
  protected final int connectionCounterValue;
  protected final Collection<ConnectionListener> connectionListeners = new CopyOnWriteArrayList();
  protected SmackDebugger debugger;
  protected final Map<PacketInterceptor, InterceptorWrapper> interceptors;
  protected Reader reader;
  protected final Map<PacketListener, ListenerWrapper> recvListeners;
  protected RosterStorage rosterStorage;
  protected SASLAuthentication saslAuthentication;
  protected final Map<PacketListener, ListenerWrapper> sendListeners;
  protected Writer writer;

  static
  {
    DEBUG_ENABLED = false;
    try
    {
      DEBUG_ENABLED = Boolean.getBoolean("smack.debugEnabled");
      SmackConfiguration.getVersion();
      return;
    }
    catch (Exception localException)
    {
    }
  }

  protected Connection(ConnectionConfiguration paramConnectionConfiguration)
  {
    this.collectors = new ConcurrentLinkedQueue();
    this.recvListeners = new ConcurrentHashMap();
    this.sendListeners = new ConcurrentHashMap();
    this.interceptors = new ConcurrentHashMap();
    this.accountManager = null;
    this.chatManager = null;
    this.debugger = null;
    this.saslAuthentication = new SASLAuthentication(this);
    this.connectionCounterValue = connectionCounter.getAndIncrement();
    this.config = paramConnectionConfiguration;
  }

  public static void addConnectionCreationListener(ConnectionCreationListener paramConnectionCreationListener)
  {
    connectionEstablishedListeners.add(paramConnectionCreationListener);
  }

  protected static Collection<ConnectionCreationListener> getConnectionCreationListeners()
  {
    return Collections.unmodifiableCollection(connectionEstablishedListeners);
  }

  public static void removeConnectionCreationListener(ConnectionCreationListener paramConnectionCreationListener)
  {
    connectionEstablishedListeners.remove(paramConnectionCreationListener);
  }

  public void addConnectionListener(ConnectionListener paramConnectionListener)
  {
    if (!(isConnected()))
      throw new IllegalStateException("Not connected to server.");
    if (paramConnectionListener == null);
    while (true)
    {
      do
        return;
      while (this.connectionListeners.contains(paramConnectionListener));
      this.connectionListeners.add(paramConnectionListener);
    }
  }

  public void addPacketInterceptor(PacketInterceptor paramPacketInterceptor, PacketFilter paramPacketFilter)
  {
    if (paramPacketInterceptor == null)
      throw new NullPointerException("Packet interceptor is null.");
    this.interceptors.put(paramPacketInterceptor, new InterceptorWrapper(paramPacketInterceptor, paramPacketFilter));
  }

  public void addPacketListener(PacketListener paramPacketListener, PacketFilter paramPacketFilter)
  {
    if (paramPacketListener == null)
      throw new NullPointerException("Packet listener is null.");
    ListenerWrapper localListenerWrapper = new ListenerWrapper(paramPacketListener, paramPacketFilter);
    this.recvListeners.put(paramPacketListener, localListenerWrapper);
  }

  public void addPacketSendingListener(PacketListener paramPacketListener, PacketFilter paramPacketFilter)
  {
    if (paramPacketListener == null)
      throw new NullPointerException("Packet listener is null.");
    ListenerWrapper localListenerWrapper = new ListenerWrapper(paramPacketListener, paramPacketFilter);
    this.sendListeners.put(paramPacketListener, localListenerWrapper);
  }

  public abstract void connect()
    throws XMPPException;

  public PacketCollector createPacketCollector(PacketFilter paramPacketFilter)
  {
    PacketCollector localPacketCollector = new PacketCollector(this, paramPacketFilter);
    this.collectors.add(localPacketCollector);
    return localPacketCollector;
  }

  public void disconnect()
  {
    disconnect(new Presence(Presence.Type.unavailable));
  }

  public abstract void disconnect(Presence paramPresence);

  protected void firePacketInterceptors(Packet paramPacket)
  {
    Iterator localIterator;
    if (paramPacket != null)
      localIterator = this.interceptors.values().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      ((InterceptorWrapper)localIterator.next()).notifyListener(paramPacket);
    }
  }

  protected void firePacketSendingListeners(Packet paramPacket)
  {
    Iterator localIterator = this.sendListeners.values().iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      ((ListenerWrapper)localIterator.next()).notifyListener(paramPacket);
    }
  }

  public AccountManager getAccountManager()
  {
    if (this.accountManager == null)
      this.accountManager = new AccountManager(this);
    return this.accountManager;
  }

  public String getCapsNode()
  {
    return this.config.getCapsNode();
  }

  /**
   * @deprecated
   */
  public ChatManager getChatManager()
  {
    monitorenter;
    try
    {
      if (this.chatManager == null)
        this.chatManager = new ChatManager(this);
      ChatManager localChatManager = this.chatManager;
      monitorexit;
      return localChatManager;
    }
    finally
    {
      localObject = finally;
      monitorexit;
      throw localObject;
    }
  }

  protected ConnectionConfiguration getConfiguration()
  {
    return this.config;
  }

  public abstract String getConnectionID();

  protected Collection<ConnectionListener> getConnectionListeners()
  {
    return this.connectionListeners;
  }

  public String getHost()
  {
    return this.config.getHost();
  }

  protected Collection<PacketCollector> getPacketCollectors()
  {
    return this.collectors;
  }

  protected Map<PacketInterceptor, InterceptorWrapper> getPacketInterceptors()
  {
    return this.interceptors;
  }

  protected Map<PacketListener, ListenerWrapper> getPacketListeners()
  {
    return this.recvListeners;
  }

  protected Map<PacketListener, ListenerWrapper> getPacketSendingListeners()
  {
    return this.sendListeners;
  }

  public int getPort()
  {
    return this.config.getPort();
  }

  public abstract Roster getRoster();

  public SASLAuthentication getSASLAuthentication()
  {
    return this.saslAuthentication;
  }

  public String getServiceName()
  {
    return this.config.getServiceName();
  }

  public abstract String getUser();

  protected void initDebugger()
  {
    Object localObject1;
    Object localObject2;
    if ((this.reader == null) || (this.writer == null))
      throw new NullPointerException("Reader or writer isn't initialized.");
    if (this.config.isDebuggerEnabled())
    {
      if (this.debugger != null)
        break label238;
      localObject1 = null;
    }
    try
    {
      String str = System.getProperty("smack.debuggerClass");
      localObject1 = str;
      localObject2 = null;
      label238: if (localObject1 != null);
    }
    catch (Throwable localThrowable)
    {
      try
      {
        Class localClass3 = Class.forName(localObject1);
        localObject2 = localClass3;
        if (localObject2 == null);
      }
      catch (Throwable localThrowable)
      {
        try
        {
          Class localClass2 = Class.forName("de.measite.smack.AndroidDebugger");
          localObject2 = localClass2;
        }
        catch (Exception localException2)
        {
          while (true)
          {
            try
            {
              while (true)
              {
                Class[] arrayOfClass = new Class[3];
                arrayOfClass[0] = Connection.class;
                arrayOfClass[1] = Writer.class;
                arrayOfClass[2] = Reader.class;
                Constructor localConstructor = localObject2.getConstructor(arrayOfClass);
                Object[] arrayOfObject = new Object[3];
                arrayOfObject[0] = this;
                arrayOfObject[1] = this.writer;
                arrayOfObject[2] = this.reader;
                this.debugger = ((SmackDebugger)localConstructor.newInstance(arrayOfObject));
                this.reader = this.debugger.getReader();
                this.writer = this.debugger.getWriter();
                return;
                localException4 = localException4;
                localException4.printStackTrace();
              }
              localException2 = localException2;
            }
            catch (Exception localException1)
            {
              try
              {
                Class localClass1 = Class.forName("org.jivesoftware.smack.debugger.ConsoleDebugger");
                localObject2 = localClass1;
              }
              catch (Exception localException3)
              {
                while (true)
                  localException3.printStackTrace();
                localException1 = localException1;
                throw new IllegalArgumentException("Can't initialize the configured debugger!", localException1);
              }
            }
            this.reader = this.debugger.newConnectionReader(this.reader);
            this.writer = this.debugger.newConnectionWriter(this.writer);
          }
          localThrowable = localThrowable;
        }
      }
    }
  }

  public abstract boolean isAnonymous();

  public abstract boolean isAuthenticated();

  public abstract boolean isConnected();

  protected boolean isReconnectionAllowed()
  {
    return this.config.isReconnectionAllowed();
  }

  public abstract boolean isSecureConnection();

  public boolean isSendPresence()
  {
    return this.config.isSendPresence();
  }

  public abstract boolean isUsingCompression();

  public void login(String paramString1, String paramString2)
    throws XMPPException
  {
    login(paramString1, paramString2, "Smack");
  }

  public abstract void login(String paramString1, String paramString2, String paramString3)
    throws XMPPException;

  public abstract void loginAnonymously()
    throws XMPPException;

  public void removeConnectionListener(ConnectionListener paramConnectionListener)
  {
    this.connectionListeners.remove(paramConnectionListener);
  }

  protected void removePacketCollector(PacketCollector paramPacketCollector)
  {
    this.collectors.remove(paramPacketCollector);
  }

  public void removePacketInterceptor(PacketInterceptor paramPacketInterceptor)
  {
    this.interceptors.remove(paramPacketInterceptor);
  }

  public void removePacketListener(PacketListener paramPacketListener)
  {
    this.recvListeners.remove(paramPacketListener);
  }

  public void removePacketSendingListener(PacketListener paramPacketListener)
  {
    this.sendListeners.remove(paramPacketListener);
  }

  public abstract void sendPacket(Packet paramPacket);

  public abstract void setRosterStorage(RosterStorage paramRosterStorage)
    throws IllegalStateException;

  protected static class InterceptorWrapper
  {
    private PacketFilter packetFilter;
    private PacketInterceptor packetInterceptor;

    public InterceptorWrapper(PacketInterceptor paramPacketInterceptor, PacketFilter paramPacketFilter)
    {
      this.packetInterceptor = paramPacketInterceptor;
      this.packetFilter = paramPacketFilter;
    }

    public boolean equals(Object paramObject)
    {
      boolean bool;
      if (paramObject == null)
        bool = false;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return bool;
            if (!(paramObject instanceof InterceptorWrapper))
              break;
            bool = ((InterceptorWrapper)paramObject).packetInterceptor.equals(this.packetInterceptor);
          }
          if (!(paramObject instanceof PacketInterceptor))
            break;
          bool = paramObject.equals(this.packetInterceptor);
        }
        bool = false;
      }
    }

    public void notifyListener(Packet paramPacket)
    {
      if ((this.packetFilter == null) || (this.packetFilter.accept(paramPacket)))
        this.packetInterceptor.interceptPacket(paramPacket);
    }
  }

  protected static class ListenerWrapper
  {
    private PacketFilter packetFilter;
    private PacketListener packetListener;

    public ListenerWrapper(PacketListener paramPacketListener, PacketFilter paramPacketFilter)
    {
      this.packetListener = paramPacketListener;
      this.packetFilter = paramPacketFilter;
    }

    public void notifyListener(Packet paramPacket)
    {
      if ((this.packetFilter == null) || (this.packetFilter.accept(paramPacket)))
        this.packetListener.processPacket(paramPacket);
    }
  }
}